#include "host_config.h"
#include "host_buf.h"
#include "papi.h"

#include "bt_ctrl.h"
#include "bt_msg.h"

#include "gatt.h"
#include "manager.h"

#define ATT_UUID_IAS	0x1802

#define ATT_UUID_ALERT_LEVEL	0x2A06

static const u_int8 fms_ias_service_uuid[2] = {ATT_UUID_IAS&0xff, ATT_UUID_IAS>>8};
static gatt_chara_def_short_t fms_al_char = {ATT_CHARA_PROP_WWP, 0, 0, ATT_UUID_ALERT_LEVEL&0xff, ATT_UUID_ALERT_LEVEL>>8};
static char alert_level[10];

//Immediate Alert Service
static const gatt_element_t fms_ias[] =
{
	{2, ATT_PM_READABLE, ATT_UUID_PRIMARY, ATT_FMT_SHORT_UUID|ATT_FMT_GROUPED, (void*)fms_ias_service_uuid, NULL},
	{sizeof(fms_al_char), ATT_PM_READABLE, ATT_UUID_CHAR, ATT_FMT_SHORT_UUID, (void*)&fms_al_char, NULL},
	{sizeof(alert_level), 0, ATT_UUID_ALERT_LEVEL, ATT_FMT_WRITE_NOTIFY, (void*)alert_level, NULL},
};

static char fms_dn[] = "rdamicro fms";

void fms_init()
{
	int i,index = 0;
	u_int8 adv[31];

	adv[index++] = 0x03;
	adv[index++] = 0x03;
	BT_WRITE_LE_UINT16(adv+index, ATT_UUID_IAS);
	index += 2;

	adv[index++] = sizeof(fms_dn)+1;
	adv[index++] = 0x09;
	pMemcpy(adv+index, fms_dn, sizeof(fms_dn));
	index += sizeof(fms_dn);
	MGR_LESetAdvData(adv, index);
	MGR_LESetAdvIntval(0x0020, 0x0040);
	MGR_LESetAdvertMode(2, 0, 0);
	
	//for(i=0;i<10;i++)
	//	alert_level[i] = i;

	gatt_add_service(fms_ias, sizeof(fms_ias)/sizeof(gatt_element_t));
}